package com.itheima.reggie.controller;

import cn.hutool.core.util.RandomUtil;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.SmsTemplate;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //获取验证码
    @PostMapping("/user/sendMsg")
    public ResultInfo sendMsg(@RequestBody Map<String,String> map){

        //1.接收参数
        String phone = map.get("phone");

        //2.生成验证码
        //String code = RandomUtil.randomNumbers(6);
        //todo 为了测试方便 验证码写死成123
        String code = "123";
        log.info("生成的code为:{}",code);

        //3. 将验证码保存到session中
        //httpSession.setAttribute("SMS_" + phone,code);
        redisTemplate.opsForValue().set("SMS_" + phone,code,5, TimeUnit.MINUTES);

        //4. 发送验证码到手机  todo 此代码注释 基于老师的阿里云发送
        //smsTemplate.sendSms(phone,code);

        return ResultInfo.success(null);
    }

    //用户登录注册
    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String,String> map){
        //1.接收参数
        String phone = map.get("phone");
        String code = map.get("code");

        //2.校验code  前端传入code  VS  session保存code
//        String codeFromSession = (String) httpSession.getAttribute("SMS_" + phone);
//        if(!StringUtils.equals(code,codeFromSession)
//                ||StringUtils.isEmpty(code)
//                ||StringUtils.isEmpty(codeFromSession)){
//            return ResultInfo.error("您输入的验证码有误");
//        }

        String codeFromRedis = (String) redisTemplate.opsForValue().get("SMS_" + phone);
        if(!StringUtils.equals(code,codeFromRedis)
                ||StringUtils.isEmpty(code)
                ||StringUtils.isEmpty(codeFromRedis)){
            return ResultInfo.error("您输入的验证码有误");
        }
        //3. 根据phone从数据库查询
        User user = userService.findByPhone(phone);
        if(user != null){
            //3-1 说明是老用户,直接过
        }else {
            //3-2 没查到,代表新用户在注册
           user  = new User();

           user.setPhone(phone);
           user.setStatus(1);//激活
           userService.save(user);
        }

        //4.将用户信息保存到session中
        // SESSION_USER 这个键必须跟拦截器中使用的键一致
        httpSession.setAttribute("SESSION_USER",user);

        //5.返回成功标识(注意不要忘记返回user,前端要用)
        return ResultInfo.success(user);
    }

    //用户退出
    @PostMapping("/user/logout")
    public ResultInfo logout(){
        httpSession.invalidate();
        return ResultInfo.success(null);
    }
}
